package com.qqtech.qquan.order.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.hi.service.HiOrderService;
import com.qqtech.qquan.order.enums.OrderBaseOrderTypeDictEnum;
import com.qqtech.qquan.order.model.OrderBase;
import com.qqtech.qquan.order.model.OrderBaseQuery;
import com.qqtech.qquan.order.model.OrderBaseVo;
import com.qqtech.qquan.order.model.OrderDelivery;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.order.service.OrderDeliveryService;
import com.qqtech.qquan.order.service.OrderHandleService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/order/handle")
public class OrderHandleController extends BaseWebController<OrderBase, OrderBaseQuery> {
	@Resource
	private OrderBaseService orderBaseService;
	@Resource
	private OrderHandleService orderHandleService;
	@Resource
	private OrderDeliveryService orderDeliveryService;
	@Resource
	private HiOrderService hiOrderService;

	@Override
	protected BaseService<OrderBase> getBaseService() {
		return orderBaseService;
	}

	/**
	 * 买家取消订单
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/buyerCancel")
	@ResponseBody
	public Result buyerCancel(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		OrderBase orderbase = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
		if (super.getUserId().intValue() != orderbase.getBuyerId()) {
			result.addErr("此订单不是您的，请勿操作");
			return result;
		}
		result = orderHandleService.buyerCancel(orderBaseQuery.getOrderNo());
		return result;
	}

	/**
	 * 买家签收订单
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/signed")
	@ResponseBody
	public Result signed(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		OrderBase orderbase = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
		if (super.getUserId().intValue() != orderbase.getBuyerId()) {
			result.addErr("此订单不是您的，请勿操作");
			return result;
		}
		result = orderHandleService.signed(orderBaseQuery.getOrderNo());
		return result;
	}

	/**
	 * 买家申请退货(同时退款)
	 * 
	 * @param orderDelivery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/applyReturn")
	@ResponseBody
	public Result applyReturn(@RequestBody OrderDelivery orderDelivery) {
		Result result = new Result();
		OrderBase orderbase = orderBaseService.getByOrderNo(orderDelivery.getOrderNo());
		if (super.getUserId().intValue() != orderbase.getBuyerId()) {
			result.addErr("此订单不是您的，请勿操作");
			return result;
		}
		result = orderHandleService.applyReturn(orderDelivery);
		return result;
	}

	/**
	 * 卖家发货
	 * 
	 * @param orderNo
	 *            订单号
	 * @param hasNeedDelivery
	 *            是否需要物流：1需要，2不需要
	 * @param deliveryName
	 *            快递或物流公司名称(家政订单可以为空)
	 * @param deliveryNo
	 *            快递或物流单号(家政订单可以为空)
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/sended")
	@ResponseBody
	public Result sended(@RequestBody Map<String, String> map) {
		Result result = new Result();
		OrderBase orderbase = orderBaseService.getByOrderNo(map.get("orderNo"));
		if (super.getUserId().intValue() != orderbase.getSellerId()) {
			result.addErr("此订单卖家不是您，请勿操作");
			return result;
		}
		Integer hasNeedDelivery = Integer.valueOf(map.get("hasNeedDelivery"));
		String orderNo = map.get("orderNo");
		String deliveryName = map.get("deliveryName");
		String deliveryNo = map.get("deliveryNo");
		Integer sendDeliveryType = null;
		//不需要物流（需要配送）
		OrderDelivery delivery = orderDeliveryService.getByOrderNo(orderbase.getOrderNo());
		
		if(delivery != null && delivery.getSendDeliveryType() != null){
			sendDeliveryType = delivery.getSendDeliveryType(); //对服务订单有无影响 不需要物流？
		}else{
			result.addErr("订单配送信息异常，请联系客服处理");
			return result;
		}
		
		/*if (map.containsKey("sendDeliveryType") && hasNeedDelivery.intValue() == YesNoEnum.NO.getCode()) {
			sendDeliveryType = Integer.valueOf(map.get("sendDeliveryType"));
		} else if (hasNeedDelivery.intValue() == YesNoEnum.NO.getCode()) {
			sendDeliveryType = OrderDeliverySendDeliveryTypeDictEnum.SELLER.getCode();
		}*/
		result = orderHandleService.sended(orderNo, hasNeedDelivery, sendDeliveryType, deliveryName, deliveryNo);
		return result;
	}

	/**
	 * 卖家取消
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/sellerCancel")
	@ResponseBody
	public Result sellerCancel(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		OrderBase orderbase = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
		if (super.getUserId().intValue() != orderbase.getSellerId()) {
			result.addErr("此订单不是您的，请勿操作");
			return result;
		}
		result = orderHandleService.sellerCancel(orderBaseQuery.getOrderNo());
		return result;
	}

	@RequestMapping(value = "/autoCancelBeforePay")
	@ResponseBody
	public Result autoCancelBeforePay() {
		try {
			orderHandleService.autoCancelBeforePay();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("在线支付订单,自动取消时发生异常-" + e.getMessage());
		}
		return new Result();
	}

	@RequestMapping(value = "/autoSign")
	@ResponseBody
	public Result autoSign() {
		try {
			orderHandleService.autoSign();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("在线支付订单,付款X天未确认收货,自动确认收货时发生异常-" + e.getMessage());
		}
		return new Result();
	}

	@RequestMapping(value = "/autoReturned")
	@ResponseBody
	public Result autoReturned() {
		try {
			orderHandleService.autoReturned();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("在线支付订单,买家申请退款X天后卖家未同意退款,自动退款时发生异常-" + e.getMessage());
		}
		return new Result();
	}
	
	/**
	 * 配送人员点击 取件
	 * @param orderBaseVo
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/pickuped")
	@ResponseBody
	public Result pickuped(@RequestBody OrderBaseVo orderBaseVo) {
		Result result = new Result();
		if (orderBaseVo == null) {
			result.addErr("参数不能为空");
		} else {
			if (StringUtil.isBlank(orderBaseVo.getOrderNo())) {
				result.addErr("订单号不能为空");
			}
			if (result.checkForm() && (orderBaseVo.getSendDeliveryOrderList() == null || orderBaseVo.getSendDeliveryOrderList().isEmpty())) {
				result.addErr("请勾选配送单");
			}
		}
		if (result.checkForm()) {
			result = orderHandleService.pickuped(orderBaseVo);
		}
		return result;
	}
	
	/**
	 * 配送人员点击 配送
	 * @param orderBaseVo
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/deliveryed")
	@ResponseBody
	public Result deliveryed(@RequestBody OrderBaseVo orderBaseVo) {
		Result result = new Result();
		if (orderBaseVo == null) {
			result.addErr("参数不能为空");
		} else {
			if (StringUtil.isBlank(orderBaseVo.getOrderNo())) {
				result.addErr("订单号不能为空");
			}
			/*if (result.checkForm() && (orderBaseVo.getSendDeliveryOrderList() == null || orderBaseVo.getSendDeliveryOrderList().isEmpty())) {
				result.addErr("请勾选配送单");
			}*/
		}
		if (result.checkForm()) {
			result = orderHandleService.deliveryed(orderBaseVo);
		}
		return result;
	}
	
	/**
	 * 买家申请退款
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/applyReturnPay")
	@ResponseBody
	public Result applyReturnPay(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if(orderBaseQuery.getOrderNo() == null){
			result.addErr("订单不存在");
			return result;
		}
		OrderBase orderbase = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
		if (super.getUserId().intValue() != orderbase.getBuyerId()) {
			result.addErr("此订单不是您的，请勿操作");
			return result;
		}
		result = orderHandleService.applyReturnPay(orderbase.getOrderNo());
		return result;
	}
	
	/**
	 * 卖家同意退款
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/sellerAgreeReturnPay")
	@ResponseBody
	public Result sellerAgreeReturnPay(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if(orderBaseQuery.getOrderNo() == null){
			result.addErr("订单不存在");
			return result;
		}
		OrderBase orderbase = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
		if (super.getUserId().intValue() != orderbase.getSellerId()) {
			result.addErr("此订单不是您的，请勿操作");
			return result;
		}
		result = orderHandleService.sellerAgreeReturnPay(orderbase.getOrderNo());
		return result;
	}
	
	/**
	 * 卖家拒绝退款
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/sellerRefuseReturnPay")
	@ResponseBody
	public Result sellerRefuseReturnPay(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if(orderBaseQuery.getOrderNo() == null){
			result.addErr("订单不存在");
			return result;
		}
		OrderBase orderbase = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
		if (super.getUserId().intValue() != orderbase.getSellerId()) {
			result.addErr("此订单不是您的，请勿操作");
			return result;
		}
		result = orderHandleService.sellerRefuseReturnPay(orderbase.getOrderNo());
		return result;
	}
	
	/**
	 * 圈圈后台同意退款
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/adminAgreeReturnPay")
	@ResponseBody
	public Result adminAgreeReturnPay(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if(orderBaseQuery.getOrderNo() == null){
			result.addErr("订单不存在");
			return result;
		}
		OrderBase orderbase = orderBaseService.getByOrderNo(orderBaseQuery.getOrderNo());
		result = orderHandleService.adminAgreeReturnPay(orderbase.getOrderNo());
		return result;
	}
}
